package utils
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	/**
	 * 位图动画
	 * @author 青竹
	 * 
	 */
	public class Animation extends BitmapAnimation
	{
		private static var dic:Dictionary=new Dictionary();
		private var _id:Object;
		private var _isShare:Boolean=true;
		private static var animations:Vector.<Animation>=new Vector.<Animation>();
		private static var ids:Vector.<String>=new Vector.<String>
		private static var timer:Timer;
		private static var _isPlaying:Boolean=false;
		private static var onStateAnis:Vector.<String>=new Vector.<String>();
		private static var count:int=0;
		public static var frameRote:int=12;//20
//		private var isSize:Boolean;
//		private var _rows:int;
//		private var _cols:int;
		/**
		 *
		 * @param bitmapData
		 * @param rows
		 * @param cols
		 * @param tolFrame
		 * @param isSize 指明rows,cols是行列还是宽高。
		 *
		 */
		public function Animation(id:String,rows:Number,cols:Number,tolFrame:int=0,isSize:Boolean=false,bd:BitmapData=null)
		{
			//isSize=isSize;
			_id=id;
//			_rows=rows;
//			_cols=cols;
			if(dic.hasOwnProperty(_id)){
				begin(dic[_id],rows,cols,tolFrame,isSize);
			}else{
				_isShare=false;
				if(bd==null){
				   return;
				}
				///throw Error("未共享且图橡数据为空，不能创建动画.");
				//resetBitmapData(bd);
				begin(bd,rows,cols,tolFrame,isSize);
				start();
				this.reader();
			}
			this.addEventListener(Event.ADDED_TO_STAGE,onStage)
		}

		public static function get isPlaying():Boolean
		{
			return _isPlaying;
		}

		public function dispose():void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onStage)
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemove)
		}
		
		private function onStage(e:Event):void{
			onStateAnis.push(_id);
		}
		
		private function onRemove(e:Event):void{
			var i:int=onStateAnis.indexOf(_id);
			if(i>=0){
				onStateAnis.splice(i,1);
				i=onStateAnis.indexOf(_id);
			}
			if(i>=0) return;
			delete	dic[_id];
			i= animations.indexOf(this)
			if(i>=0){
				animations.splice(i,1);
			}
		}
		
		private static function start():void{
			if(_isPlaying || _isPause) return;
			_isPlaying=true;
			if(timer==null){
				timer=new Timer(1000/frameRote);
				timer.addEventListener(TimerEvent.TIMER,onTime);
			}
			timer.start();
		}
		
		/**
		 * 停止渲染
		 *
		 */
		public function remove():void{
		    var index:int=animations.indexOf(bitmap);
			if(index>0){
				animations.splice(index,1);
			}
		}
		private static function onTime(e:TimerEvent):void{
			var len:int=animations.length;
			for(var i:int=0;i<len;i++){
				animations[i].reader();
			}
		}
		public static function dispose():void{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,onTime);
			timer=null;
			_isPlaying=false;
			animations=null;
		}
		private static var _isPause:Boolean
		public static function stopAll():void{
			//if(!_isPlaying) return
			_isPause=true
			if(timer){
			    timer.stop();
				_isPlaying=false;
			}
		}
		public static function playAll():void{
			_isPause=false;
			if(_isPlaying ) return;
			
			if(timer==null){
				start();
			}else{
				timer.start();
			}
			_isPlaying=true;
			
		}
		
		override protected function init():void{
			  if(_isShare){
				  this.bitmapData=dic[_id] as BitmapData;
			  }else{
				  super.init();
				  dic[_id]=this.bitmap;
				  if(ids.indexOf(_id)==-1){
					  ids.push(_id);
					  animations.push(this);
					  trace("当前位图动画BitmapData总数:"+animations.length,_id)
				  }
			  }
		}
	}
}